<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="750" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">

			<el-form-item label="标题" prop="title">
				<el-input v-model="form.title" clearable placeholder="必填 如：新人专享券"></el-input>
			</el-form-item>
            <el-form-item label="面值" prop="mian_money">
				<el-input v-model="form.mian_money" clearable placeholder=""></el-input>
			</el-form-item>
            <el-form-item label="使用要求" prop="require_type">
                <el-radio-group v-model="form.require_type">
                    <el-radio :label="0">满足金额可使用</el-radio>
                    <el-radio :label="1">无限制</el-radio>
                </el-radio-group>
			</el-form-item>
            <el-form-item label="满足金额" prop="require_money" v-if="form.require_type == 0">
				<el-input v-model="form.require_money" clearable placeholder=""></el-input>
			</el-form-item>
            <el-form-item label="简介" prop="des">
				<el-input v-model="form.des" clearable placeholder=""></el-input>
			</el-form-item>
            <el-form-item label="到期类型" prop="time_type">
                <el-radio-group v-model="form.time_type">
                    <el-radio :label="0">永久有效</el-radio>
                    <el-radio :label="1">固定到期时间</el-radio>
                    <el-radio :label="2">领取后几日内到期</el-radio>
                </el-radio-group>
			</el-form-item>
            <el-form-item label="到期时间" prop="end_time" v-if="form.time_type==1">
                <el-date-picker
                    v-model="form.end_time"
                    type="datetime"
                    placeholder="选择到期时间">
                </el-date-picker>

			</el-form-item>
            <el-form-item label="到期天数" prop="end_day" v-if="form.time_type==2">
				<el-input v-model="form.end_day" clearable></el-input>
			</el-form-item>

			<el-form-item label="新人专享">
				<el-switch v-model="form.newpeople" active-value="1" inactive-value="0"></el-switch>
				<div class="el-form-item-msg" data-v-b33b3cf8="">新人专享即针对从未在平台下单的用户，已下过单的用户则无法领取</div>
			</el-form-item>
			<el-form-item label="详细" prop="content">
				<el-card shadow="never">
					<sc-editor v-model="form.details" placeholder="请输入" :templates="templates" :height="400"></sc-editor>
				</el-card>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
    import { defineAsyncComponent } from 'vue';
	const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
	export default {
		components: {
			scEditor
		},
		emits: ['success', 'closed'],
		data() {
			return {
				cat:[],
                guige:false,
                guigelist:[
                    {'can':'','pro_price':'','pro_original_price':'','pro_stock':''}
                ],
				mode: "add",
				titleMap: {
					add: '新增优惠券',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
                    'require_type':0
                },
				//验证规则
				rules: {
					title: [
						{required: true, message: '请输入标题'}
					],
                    mian_money:[
                    {required: true, message: '请输入面值'}
                    ]
				}
			}
		},
		mounted() {
			 this.getcat()
		},
		methods: {
			async getcat(){
				var res = await this.$API.system.goods.cat.get();
				if(res.code == 200){
					 	this.cat = res.data.rows
				}
			},
			async delSpec(id,dex){
				var reqData = {id:id,'act':'del'}
				//var res = await this.$API.demo.goodsspec_del.post(reqData);
				var res = await this.$API.system.universal.handle.post(reqData,'/goods/goodsspec_del');
				if(res.code == 200){
					this.$message.success("删除成功")
					this.guigelist.splice(dex,1)
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},
            addguige(){
                this.guigelist.push( {'can':'','pro_price':'','pro_original_price':'','pro_stock':''})
            },
            delguige(dex){
				if(this.guigelist[dex].id=='' || this.guigelist[dex].id == null){
					 this.guigelist.splice(dex,1)
				}else{
					 this.delSpec(this.guigelist[dex].id,dex)
				}

            },
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.system.universal.handle.post(this.form,'/mk/coupon_save');
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				if(this.mode == 'edit'){
					 this.guigelist = data.guigedata
					 this.guige = data.guige==1?true:false
				}
				this.form = data
			}
		}
	}
</script>

<style>
</style>
